package it.laypay.admin;

import org.json.JSONObject;
import org.openqa.selenium.By;

import java.util.HashMap;
import java.util.Map;

/**
 * User: Aruna Rajapaksha
 */

public class CreateNewBusinessPage extends AdminPage {

    //Elements
    String pageTitle = "Create New Business";
    By displayName = By.id("DisplayName");
    By businessName = By.id("BusinessName");
    By australianBusinessNo = By.id("AustralianBusinessNo");
    By accountID = By.id("AccountID");
    By businessType = By.id("BusinessTypeID");
    By title = By.id("TitleId");
    By firstName = By.id("FirstName");
    By middleName = By.id("MiddleName");
    By lastName = By.id("LastName");
    By email = By.id("Email");
    By password = By.id("Password");
    By confirmPassword = By.id("ConfirmPassword");
    By businessPhoneNo = By.id("BusinessPhoneNo");
    By mobileNo = By.id("MobileNo");
    By maxTransactionAmount = By.id("MaxTransactionAmount");
    By minTransactionAmount = By.id("MinTransactionAmount");
    By termsAndConditions = By.id("TermsAndConditions");
    By logoSmall = By.id("LogoSmall");
    By logoLarge = By.id("LogoLarge");
    Map<String, String> paymentType = new HashMap<String, String>();
    Map<String, String> installmentType = new HashMap<String, String>();
    By create = By.cssSelector("input[type=\"submit\"]");

    By errorValidations = By.className("validation-summary-errors");
    String response = "^[\\s\\S]*Merchant successfully saved[\\s\\S]*$";
    By errorAlert = By.className("alert");

    String expectedLabel = "Display Name,Business Name,Australian Business No,Account ID,Business Type,Title," +
            "First Name,Middle Name,Last Name,E-mail,Password,Confirm Password,Business Phone No,Mobile No," +
            "MaxTransactionAmount,MinTransactionAmount,Terms and Conditions,Small Logo,Large Logo,Payment Types," +
            "Direct Debit,Visa Card,MasterCard,Installment Types,Weekly,Fortnightly,Monthly,Quarterly,Annually,None";

    public CreateNewBusinessPage() {

        paymentType.put("Direct Debit", "1");
        paymentType.put("Visa Card", "2");
        paymentType.put("MasterCard", "3");

        installmentType.put("Weekly", "2");
        installmentType.put("Fortnightly", "3");
        installmentType.put("Monthly", "4");
        installmentType.put("Quarterly", "5");
        installmentType.put("Annually", "6");
        installmentType.put("Annually", "6");
        installmentType.put("None", "7");
    }

    private void setPaymentTypes(String paymentTypes) throws Exception {

        String[] paymentType = paymentTypes.split(DATA_DELIMITER);
        for (String name : paymentType) {
            click(By.id("pay" + this.paymentType.get(name.trim())));
        }
    }

    private void setInstallmentType(String installmentTypes) throws Exception {

        String[] installmentType = installmentTypes.split(DATA_DELIMITER);
        for (String name : installmentType) {
            click(By.id("ins" + this.installmentType.get(name.trim())));
        }
    }

    public void validateLabels() throws Exception {

        driver.get(ADMIN_URL);
        click(createMerchant);
        checkPageTitle(pageTitle);
        validateLabels(expectedLabel);
    }

    public void create(JSONObject data) throws Exception {

        removeEmail(data.getString("Email"), ADMIN_URL);
        click(createMerchant);
        checkPageTitle(pageTitle);

        clearAndType(displayName, data.getString("DisplayName"));
        clearAndType(businessName, data.getString("BusinessName"));
        clearAndType(australianBusinessNo, data.getString("AustralianBusinessNo"));
        clearAndType(accountID, data.getString("AccountID"));
        selectDropDown(businessType, data.getString("BusinessType"));
        selectDropDown(title, data.getString("Title"));
        clearAndType(firstName, data.getString("FirstName"));
        clearAndType(middleName, data.getString("MiddleName"));
        clearAndType(lastName, data.getString("LastName"));
        clearAndType(email, data.getString("Email"));
        clearAndType(password, data.getString("Password"));
        clearAndType(confirmPassword, data.getString("Password"));
        clearAndType(businessPhoneNo, data.getString("BusinessPhoneNo"));
        clearAndType(mobileNo, data.getString("MobileNo"));
        clearAndType(maxTransactionAmount, data.getString("MaxTransactionAmount"));
        clearAndType(minTransactionAmount, data.getString("MinTransactionAmount"));
        clearAndType(termsAndConditions, data.getString("TermsAndConditions"));
        setFilePath(logoSmall, data.getString("LogoSmall"));
        setFilePath(logoLarge, data.getString("LogoLarge"));
        setPaymentTypes(data.getString("PaymentTypes"));
        setInstallmentType(data.getString("InstallmentTypes"));

        click(create);
    }
}
